package com.hdax.dm.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.entity.order.Order;
import com.hdax.dm.entity.pay.Trade;
import com.hdax.dm.pay.config.AlipayConfig;
import com.hdax.dm.pay.feign.OrderFeign;
import com.hdax.dm.pay.feign.ScheduleSeatFeign;
import com.hdax.dm.pay.mappers.PayMapper;
import com.hdax.dm.pay.service.PayService;
import com.hdax.dm.pay.vo.TradeInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Trade> implements PayService {

    @Autowired
    private AlipayConfig alipayConfig;
    @Autowired
    private OrderFeign orderFeign;

    @Override
    public String aliPay(String orderNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                alipayConfig.getAppId(), alipayConfig.getPrivateKey(), "json",
                alipayConfig.getCharset(), alipayConfig.getPublicKey(), alipayConfig.getSignType());
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
        alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
        Order order = orderFeign.detail(orderNo);
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderNo +"\","
                + "\"total_amount\":\""+ order.getTotalAmount() +"\","
                + "\"subject\":\""+ order.getItemName() +"\","
                + "\"body\":\"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        try {
            return alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Autowired
    private ScheduleSeatFeign seatFeign;

    @Override
    public String payed(TradeInfoVo vo) {
        //根据订单好查询订单支付状态
        QueryWrapper<Trade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",vo.getOrderNo());
        Trade trade = getOne(queryWrapper);
        if(trade!=null) return "";
        Trade entity = new Trade();
        entity.setId(vo.getTradeNo());
        entity.setOrderNo(vo.getOrderNo());
        entity.setTradeNo(vo.getTradeNo());
        entity.setPayMethod(Short.valueOf("1"));
        entity.setAmount(vo.getReceiptAmount());
        save(entity);
        //修改订单
        orderFeign.addTradeNo(vo.getOrderNo(),vo.getTradeNo());
        //座位状态修改为已出售
        seatFeign.seatSold(vo.getOrderNo());
        return vo.getTradeStatus();
    }

    @Override
    public String tuikuan(String out_trade_no) {
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                alipayConfig.getAppId(), alipayConfig.getPrivateKey(), "json",
                alipayConfig.getCharset(), alipayConfig.getPublicKey(), alipayConfig.getSignType());
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        //查询订单金额
        Order order = orderFeign.selectOne(out_trade_no);
        System.out.println(out_trade_no);
        Map map = new HashMap();
        map.put("refund_amount",order.getTotalAmount());
        map.put("out_trade_no",out_trade_no);
        request.setBizContent(JSON.toJSONString(map));
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
            //改变状态已退款
            orderFeign.updateTwo(out_trade_no);
            //座位释放
            seatFeign.updateSeatOne(out_trade_no);
            request.setReturnUrl(alipayConfig.getReturnUrl());
        } else {
            System.out.println("调用失败");
        }
        return "true";
    }
}
